package com.tencent.crazypenguin.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.tencent.crazypenguin.bean.MUser;
import com.tencent.crazypenguin.bean.MatchItem;
import com.tencent.crazypenguin.bean.MatchList;
import com.tencent.crazypenguin.bean.Point;
import com.tencent.crazypenguin.bean.User;
import com.tencent.crazypenguin.config.HbmUtil;
import com.tencent.crazypenguin.config.ResultType;
import com.tencent.crazypenguin.dao.UserDao;
import net.sf.json.JSONObject;

public class GetMatchUser extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -152784141922505842L;
	
	private String result;

	@Override
	public String execute() throws Exception {
		
		JSONObject rspData = new JSONObject();
		try {
			JSONObject postData =HbmUtil.getAuthDate();
			UserDao userDao = new UserDao();
			
			long userId = postData.getLong("userId");
			
			System.out.println("ppp::"+postData.getJSONObject("origin").getDouble("latitude")+postData.getJSONObject("origin").getDouble("longitude")+
					postData.getJSONObject("target").getDouble("latitude")+postData.getJSONObject("target").getDouble("longitude"));
			MUser mUser = new MUser(postData.getJSONObject("origin").getDouble("latitude"),postData.getJSONObject("origin").getDouble("longitude"),
					postData.getJSONObject("target").getDouble("latitude"),postData.getJSONObject("target").getDouble("longitude"));
			
			List<MatchItem> matchList = MatchList.getMusers(userId,mUser);
			
			rspData.put("resultCode", ResultType.SUCEESS);
			
			if(matchList == null|| matchList.size()==0){
				rspData.put("match_user_count", 0);
				result = rspData.toString();
				return "done";
			}
			
			
			rspData.put("match_user_count", matchList.size());
			for(int i=0;i<matchList.size();i++){
				MatchItem m = matchList.get(i);
				User user = userDao.findById(m.userId, new User());
				JSONObject temp = JSONObject.fromObject(user);

				temp.put("origin", m.mUser.origin);
				temp.put("target", m.mUser.target);
				temp.put("distance", (int)m.distance);
				temp.put("type", m.Type);
				rspData.put("user"+i, temp);
			}
			
			result = rspData.toString();
			return "done";
		} catch (Exception e) {
			e.printStackTrace();
			rspData.clear();
			rspData.put("resultCode", ResultType.EXCEPTION);
			result = rspData.toString();
			return "done";
		}
	}
	
	
	
	

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	

}
